using System;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace LiveStock.Recommendation
{
    public class Stock
    {
        private string companyName;
        private string stockSymbol;
        private string exchange;
        private float lastTrade;
        private float change;
        private float pctChange;
        private float dayLow;
        private float dayHigh;
        private string volume;
        private string industry;
        private DateTime lastUpdateDate;


        public Stock()
        {
        }


        public Stock(string symbol)
        {
            stockSymbol = symbol;
        }


        public string CompanyName
        {
            get { return companyName; }
            set { companyName = value; }
        }

        public string Industry
        {
            get { return industry; }
            set { industry = value; }
        }


        public string StockSymbol
        {
            get { return stockSymbol; }
            set { stockSymbol = value; }
        }


        public string Exchange
        {
            get { return exchange; }
            set { exchange = value; }
        }


        public float LastTrade
        {
            get { return lastTrade; }
            set { lastTrade = value; }
        }


        public float Change
        {
            get { return change; }
            set { change = value; }
        }


        public float PctChange
        {
            get { return pctChange; }
            set { pctChange = value; }
        }


        public float DayLow
        {
            get { return dayLow; }
            set { dayLow = value; }
        }


        public float DayHigh
        {
            get { return dayHigh; }
            set { dayHigh = value; }
        }


        public string Volume
        {
            get { return volume; }
            set { volume = value; }
        }


        public DateTime LastUpdateDate
        {
            get { return lastUpdateDate; }
            set { lastUpdateDate = value; }
        }


        public void UpdateFromMarket()
        {
            string stockUrl = "http://finance.yahoo.com/d/quotes.csv?s=" + stockSymbol + "&f=nxsl1c6p2ghv";

            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[8192];

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(stockUrl);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;
                char[] splitter  = {','};
                do
                {
                    count = resStream.Read(buf, 0, buf.Length);

                    if (count != 0)
                    {
                        tempString = Encoding.ASCII.GetString(buf, 0, count);
                        
                        tempString = tempString.Replace("\"", "");
                        tempString = tempString.Replace("%", "");
                        tempString = tempString.Replace("N/A", "0");
                        tempString = tempString.Remove(tempString.Length - 2);
                        sb.Append(tempString);
                        string[] split = tempString.Split(splitter);
                        this.CompanyName = split[0].ToString();
                        if (this.CompanyName.Equals("")) { this.CompanyName = this.StockSymbol; }
                        this.Exchange = split[1].ToString();
                        this.StockSymbol = split[2].ToString();
                        this.LastTrade = float.Parse(split[3].ToString());
                        this.Change = float.Parse(split[4].ToString());
                        this.PctChange = float.Parse(split[5].ToString());
                        this.DayLow = float.Parse(split[6].ToString());
                        this.DayHigh = float.Parse(split[7].ToString());
                        this.Volume = split[8].ToString();
                        if (this.Exchange.Equals("0"))
                        {
                            this.CompanyName= null;
                        }
                        this.LastUpdateDate = DateTime.Now;
                    }
                }
                while (count > 0);
                resStream.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            };

           
        }


        public string ToString()
        {
            return companyName + " [" + stockSymbol + "]";
        }
    }

}